using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Matchmaking
{
    class MatchmakingClient
    {
        Socket serverSocket;
        byte[] outgoingData;

        public void ConnectToServer(string ClientName)
        {
            Console.WriteLine("Connecting "+ ClientName +" to the matchmaking server");

            if (outgoingData == null)
                outgoingData = new byte[1024];

            try
            {
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                IPHostEntry serverHostEntry = Dns.GetHostEntry("leo.servegame.org");
                serverSocket.Connect(serverHostEntry.AddressList, 10007);

                Console.WriteLine("Remote endpoint is:" + serverSocket.RemoteEndPoint.ToString());
                Console.WriteLine("Local endpoint is:" + serverSocket.LocalEndPoint.ToString());

                SendToServer("ClientID: "+ ClientName);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void SendToServer(string Data)
        {
            try
            {
                System.Text.Encoder encoder = System.Text.Encoding.UTF8.GetEncoder();
                int numBytes = encoder.GetBytes(Data.ToCharArray(), 0, Data.Length, outgoingData, 0, true);
                serverSocket.Send(outgoingData, numBytes, SocketFlags.None);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
